<?php

require_once('../include/GeneralTop.php');
require('../include/class/db.php');
require("../include/lib/fckeditor/fckeditor.php") ;

if (!$content_admin) {
	die('<div style="text-align:center">Permission denied...</div>');
}

$db = new db;
if ((isset($_POST["submit"]))) {
    $updateSQL = sprintf("UPDATE announcement SET content=%s, status=%s WHERE id=%s",
                       GetSQLValueString(stripslashes($_POST['FCKeditor1']), "text"),
                       GetSQLValueString($_POST['status'], "text"),                       
                       GetSQLValueString($_POST['id'], "int"));

    $db->query($updateSQL);
    
    if(1 === (int)$_POST['status']) {
    	$update_other = "update announcement set status=0 where id!=".$_POST['id'];
    	$db->query($update_other);
    }
    header("location:".$_SERVER['PHP_SELF']."?result=suc&id=".$_POST['id']);
}

$id = "-1";
if (isset($_GET['id'])) {
    $id = $_GET['id'];
}

$sql = sprintf("SELECT * FROM announcement WHERE id = %s", GetSQLValueString($id, "int"));
$res = $db->query($sql);
$row = $db->fetch($res);

include('AdminPanel.php');

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>编辑公告</title>
</head>

<body>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" name="form1" id="form1">
  <table align="center" width="70%">
    <?php
    if($_GET['result'] && 'suc' == $_GET['result']) {
    ?>
    <tr valign="baseline">
      <td colspan="2" nowrap="nowrap" align="center"><font color="green">更新成功...</font></td>
    </tr>
    <?php
    }
    ?>
    <tr valign="baseline">
      <td colspan="2" nowrap="nowrap" align="center"><font color="red"><b>只有一条公告会显示，所以当设置当前公告为显示的话其他的公告会自动设为不显示...</b></font></td>
    </tr>
    <tr valign="baseline">
      <td height="67" align="right" nowrap="nowrap" width="15%" valign="top">内容:</td>
      <td>
      <?php
	  $oFCKeditor = new FCKeditor('FCKeditor1') ;
	  $oFCKeditor->BasePath = FCKEDITOR_BASE_PATH;
	  $oFCKeditor->Height = 400;
	  $oFCKeditor->Value = $row['content'];
	  $oFCKeditor->Create();
	  ?>
	  </td>
    </tr>
    <tr valign="baseline">
      <td nowrap="nowrap" align="right">状态:</td>
      <td>
      <?php
      if(ANNOUNCE_DISPLAY === (int)$row['status']) {
      	  $select1 = 'selected';
      	  $select2 = '';
      }
      else if(ANNOUNCE_NOT_DISPLAY === (int)$row['status']) {
      	  $select1 = '';
      	  $select2 = 'selected';
      }
      ?>
      <select name="status">      
	      <option value="<?php echo ANNOUNCE_DISPLAY?>" <?php echo $select1?>>显示</option>
	      <option value="<?php echo ANNOUNCE_NOT_DISPLAY?>" <?php echo $select2?>>不显示</option>
	  </select>
      </td>
    </tr>
    <tr valign="baseline">
      <td nowrap="nowrap" align="right">&nbsp;</td>
      <td>
      	<input type="submit" id="submit" name="submit" value="Update record" />&nbsp;&nbsp;
        <input type="button" id="button" onclick="window.location.href='ShowAnnounce.php'" value="Back">
      </td>
    </tr>
  </table>
  <input type="hidden" name="id" value="<?php echo $id; ?>" />
</form>
<p>&nbsp;</p>
</body>
</html>